package TeacherEvaluation.com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import TeacherEvaluation.com.entity.Course;
import TeacherEvaluation.com.entity.ResultMsg;
import TeacherEvaluation.com.service.courseService;

@Controller
public class courseController {
		//导入courseService bean
		@Autowired
		courseService courseService;
		
		//添加课程主页
		@RequestMapping("/CouAddIndex")
		public ModelAndView CouAddIndex() {
			ModelAndView view = new ModelAndView("admin/course/addCourse");
			return view;
		}

		//添加课程
		@RequestMapping("/CouAdd")
		@ResponseBody
		public ResultMsg CouAdd(Course course) {
			System.out.println(course.getCourse());
			int i = courseService.insertCou(course);
			if (i==0) {
				return new ResultMsg(0, "课程已存在");
			}else {
				return new ResultMsg(1, "添加成功");
			}
		}
		
		//查看课程
		@RequestMapping("/CouSelect")
		@ResponseBody
		public ModelAndView CouSelect() {
			//创建视图
			ModelAndView mView = new ModelAndView("admin/course/showCourse");
			//将查询结果发送给视图
			mView.addObject("itmes",courseService.selectCou());
			return mView;
		}
		
		//修改课程主页
		@RequestMapping("/CouUpdateIndex")
		public ModelAndView CouUpdateIndex(Course course) {
			ModelAndView view = new ModelAndView("admin/course/updateCourse");
			view.addObject("itmes", course);
			return view;
		}
		
		
		
		//修改课程
		@RequestMapping("/CouUpdate")
		@ResponseBody
		public ResultMsg CouUpdate(Course course) {
			int i = courseService.updateCou(course);
			//查询修改的课程名是否存在
			if (i==0) {
				return new ResultMsg(0, "删除失败");
			}else {
				return new ResultMsg(1, "删除成功");
			}
		}
		
		//删除课程
		@RequestMapping("/CouDelete")
		@ResponseBody
		public ResultMsg CouDelete(String co_id) {
			int i = courseService.deleCou(Integer.parseInt(co_id));
			if (i==0) {
				return new ResultMsg(0,"删除失败");
			}else {
				return new ResultMsg(1,"删除成功");
			}
		}
}
